/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.example;

import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringSystem;
import com.tumblr.backboard.Actor;
import com.tumblr.backboard.MotionProperty;
import com.tumblr.backboard.Utils.View;
import com.tumblr.backboard.imitator.SpringImitator;
import com.tumblr.backboard.performer.Performer;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.components.element.ShapeElement;

import java.util.ArrayList;
import java.util.List;

/**
 * Demonstrates an draggable view that bounces back when released.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public class FollowFragment extends BaseFraction {
    private static final int DIAMETER = 80;
    private ComponentContainer mRootView;
    private Component mCircle;
    private Component[] mFollowers = new Component[VALUE_FOUR];

    @Override
    public int getUiContent() {
        return ResourceTable.Layout_fragment_follow;
    }

    @Override
    public void initComponent() {
        mRootView = (ComponentContainer) mComponent;
        mCircle = mRootView.findComponentById(ResourceTable.Id_circle);
        StackLayout.LayoutConfig leaderParams = (StackLayout.LayoutConfig) mCircle.getLayoutConfig();
        final List<Integer> circles = new ArrayList<>();
        circles.add(ResourceTable.Graphic_circle_green);
        circles.add(ResourceTable.Graphic_circle_red);
        circles.add(ResourceTable.Graphic_circle_purple);
        circles.add(ResourceTable.Graphic_circle_orange);
        circles.add(ResourceTable.Graphic_circle_blue);
        circles.add(ResourceTable.Graphic_circle_gray);

        // create the circle views
        int colorIndex = 1;
        for (int index = 0; index < mFollowers.length; index++) {
            mFollowers[index] = new Component(getFractionAbility());
            StackLayout.LayoutConfig params = new StackLayout.LayoutConfig(AttrHelper.vp2px(DIAMETER,
                    getFractionAbility()), AttrHelper.vp2px(DIAMETER, getFractionAbility()));
            params.alignment = leaderParams.alignment;
            mFollowers[index].setLayoutConfig(params);
            ShapeElement shapeElement = new ShapeElement(getFractionAbility(), circles.get(colorIndex));
            mFollowers[index].setBackground(shapeElement);
            colorIndex++;
            if (colorIndex >= circles.size()) {
                colorIndex = 0;
            }
            mRootView.addComponent(mFollowers[index]);
        }
        circles.clear();
        SpringSystem springSystem = SpringSystem.create();

        // create the springs that control movement
        Spring springX = springSystem.createSpring();
        Spring springY = springSystem.createSpring();

        // bind circle movement to events
        new Actor.Builder(springSystem, mCircle).addMotion(springX, MotionProperty.X)
                .addMotion(springY, MotionProperty.Y).build();

        // add springs to connect between the views
        Spring[] followsX = new Spring[mFollowers.length];
        Spring[] followsY = new Spring[mFollowers.length];

        for (int index = 0; index < mFollowers.length; index++) {
            // create spring to bind views
            followsX[index] = springSystem.createSpring();
            followsY[index] = springSystem.createSpring();
            followsX[index].addListener(new Performer(mFollowers[index], View.TRANSLATION_X));
            followsY[index].addListener(new Performer(mFollowers[index], View.TRANSLATION_Y));

            // imitates another character
            SpringImitator followX = new SpringImitator(followsX[index]);
            SpringImitator followY = new SpringImitator(followsY[index]);

            // imitate the previous character
            if (index == 0) {
                springX.addListener(followX);
                springY.addListener(followY);
            } else {
                followsX[index - 1].addListener(followX);
                followsY[index - 1].addListener(followY);
            }
        }
    }
}
